var loginApp = angular.module("loginApp",["scService"]);
loginApp.controller("loginController",function($scope,$rootScope,$http,$q,$timeout,$location,$stateParams,commonService,jsonToStr,$httpParamSerializerJQLike){
	$scope.phoneNumber;
	$scope.send = "发送验证码";
	$scope.code;
	$scope.loginPanel=false;
	$scope.codeClick=true;
	$scope.couldClick=true;
	$scope.loginIng=false;
	$scope.isTimer=false;//判断定时器是否触发，是：解除验证手机 ng-keyup=checkCodeValid()
	var openId ="";
	var code = $stateParams.code;
	//重发验证码计时器
	$scope.timer=function(wait){
		var wait=wait;
		if(wait==0){
			$scope.send="重新发送";
			wait=wait;
			$scope.isTimer=false;
			$scope.codeClick=false;
		}else{
			wait--;
			$scope.isTimer=true;
			$scope.codeClick=true;
			$scope.send=wait+"(s)"
			$timeout(function(){
				$scope.timer(wait)
			}, 1000);
		}
	}
	//点击发送验证码按钮
	$scope.sendCode = function(){
		if(!(/^1[34578]\d{9}$/.test($scope.phoneNumber))){

				$scope.toastSet({
					content:"请输入正确手机号"
				})
		}else{						
			var url = "/pub/sendVerifyCode?phone="+$scope.phoneNumber+"&merchantCode="+htConfig.merchantCode;
			commonService.commonAjaxByGet(url)
			.then(function(data){
				var datastr=JSON.stringify(data);
				//alert(datastr);
				if(data.code=="200"){
					$scope.timer(60);
					$scope.toastSet({
						content:"已发送"
					});
				}else if(data.code=="0106004"){
					$scope.toastSet({
						content:"1分钟之内只能发送一次"
					});
				}else{
					$scope.toastSet({
						content:data.msg
					});
				}
			});
		}
	};
	//验证合性改变按钮颜色
	$scope.checkCodeValid=function(){
		$scope.codeClick=(/^\d{11}$/.test($scope.phoneNumber))? false : true; 
	}
	$scope.checkValid=function(){
		$scope.couldClick=((/^1[34578]\d{9}$/.test($scope.phoneNumber))&&
			(/^(\d)+$/.test($scope.code)))? false : true;  
	}	
	//获取微信账号、openId
	$scope.getOpenId = function(){
			var state = localStorage.getItem("state");
			
			var code = $stateParams.code;
			
			 alert(code);
			var wUrl = "/pub/getAccessToken?source="+htConfig.source+"&merchantCode="+htConfig.merchantCode+"&code="+code;
			commonService.commonAjaxByGet(wUrl).then(function(data){
				var datastr=JSON.stringify(data);
					alert(datastr);
				if(data.data.openid){					
					openId = data.data.openid;
					var access_token= data.data.access_token;
					localStorage.setItem("openId",openId);
					localStorage.setItem("access_token",access_token);
					//调用自动登录
					// var num = '';
					// for(var i=0;i<8;i++){
					// 	num+=parseInt(Math.random()*10);
					// }							
						var account = openId;
						
						var autoLoginData = {
							"source":htConfig.source,
							"merchantCode":htConfig.merchantCode,
							"account":account,
							"jPushRegId":account
						};
					$rootScope.autoLogin(autoLoginData);
				}else{
					var state = localStorage.getItem("state");
					window.location.href=htConfig.hostUrl+"enter.html?state="+state
				}
			});
	};
	$scope.getOpenId();		
	//点击登录							
	$scope.login = function(){
		$scope.couldClick=true;//禁点登录	
		$scope.loginIng=true;//loding弹出	
		var postData ={
			"phone":$scope.phoneNumber,
			"verifyCode":$scope.code,
			"source":htConfig.source,
			"merchantCode":htConfig.merchantCode,
			"account":openId
		};
		commonService.commonAjaxByPost("/pub/validationVerifyCode",postData)
		.then(function(data){
			$scope.couldClick=false;//取消禁点登录
			var datastr=JSON.stringify(data);
			alert(datastr);
			if(data.code=="200"){							
				var postData ={
					"source":htConfig.source,
					"merchantCode":htConfig.merchantCode,
					"account":openId,
					"jPushRegId":openId
				};
				$rootScope.autoLogin(postData);
			}else if(data.code=="0101011"||data.code=="0101015"){
				$scope.loginIng=false;//关闭loading	
				$scope.toastSet({
					content:"验证码错误"
				});
			}else if(data.code=="0101302"){
				$scope.loginIng=false;//关闭loading	
				$scope.toastSet({
					content:"帐号已绑定"
				});
			}else{
				$scope.loginIng=false;//关闭loading	
				$scope.toastSet({
					content:"登录失败,请稍后重试"
				});
			}
		});
	}
	//点击未完成的订单
	$scope.goPay=function(){
		$location.path("orderPay/"+$scope.notPayCardNo);
	};
	//获取userCard
	$scope.getUserCard = function(){
		var userId = localStorage.getItem("userId");
		commonService.commonAjaxByGet("/user/getUserCard?userId="+userId)
		.then(function(data){
			var datastr=JSON.stringify(data);
				alert(datastr);
				$scope.loginIng=false;//登录成功关闭loading	
			if(data.code=="200"){
				localStorage.setItem("cardNo",data.data[0].cardNo)				
				var state = localStorage.getItem("state");
					if(state=="1"){
						//从‘我的’进入
						$location.path("/userInfo")
					}else if(state=="2"){
						//从‘扫码充电’进入
						$scope.getOngoingOperation();
						
					}
			}else{
				$scope.toastSet({
					content:"网络异常"
				})
			}
		})
	};
	//自动登录
	$rootScope.autoLogin=function(postData){
		commonService.commonAjaxByPost("/user/autoLogin",postData)
		.then(function(data){
				var datastr=JSON.stringify(data);
				alert(datastr);
			if(data.code=="200"){
				localStorage.setItem("userId",data.data.userId);
				localStorage.setItem("token",data.data.token);
				localStorage.setItem("phone",data.data.phone);
				
				// alert("autoLogin is ok");			
				$scope.getUserCard(); 
				
				
			}else if(data.code=="500"){
				$scope.toastSet({
					content:"系统故障"
				});
			}else if(data.code=="201"||data.code=="0101001"){
				$scope.loginPanel=true;
			}else{
				$scope.toastSet({
					content:"登录失败"
				});
			}
		})
	}	
	//登录完成后
		//查询有无未支付订单
	$rootScope.getNotPay=function(){
		var userId=localStorage.getItem("userId");
		var url = "/fund/getFundList?userId="+userId+"&pageNo=1&status=0";
		commonService.commonAjaxByGet(url).then(function(data){
			var datastr=JSON.stringify(data);
				 alert(datastr);
			if(data.code=="200"){
				if(data.data.length!=0){
					$scope.notPayCardNo = data.data[0].orderNo;
					localStorage.setItem("notPayCardNo",$scope.notPayCardNo)
					$location.path("/notPay")						
				}else{
					window.location.href=htConfig.hostUrl+"scanCode.html";
				}
			}
		});
	};
	//查询用户是否正在充电
		function isEmptyObject(e) {
			var t;
			for (t in e)
				return !1;
			return !0
		};
	$scope.getOngoingOperation=function(){
		// alert("getOngoingOperation");
		var cardNo = localStorage.getItem("cardNo");
		// alert(cardNo);
		// var url = "/charge/getOngoingOperation?userCard="+cardNo;
		// commonService.commonAjaxByGet(url).then(function(data){
		// 	var datastr=JSON.stringify(data);
		// 		// alert(datastr);
		// 	if(data.code=="200"){
		// 		if(isEmptyObject(data.data)){
		// 			$rootScope.getNotPay();
		// 		}else{
		// 			 $location.path("/charging");
		// 		}
		// 	}else{
		// 		$scope.toastSet({
		// 			content:"网络异常"
		// 		})
		// 	}
		// });
		//localStorage.setItem("openCharge",false);
		var isOpenCharging = localStorage.getItem("openCharge");
		//alert(isOpenCharging);
		if(isOpenCharging=="true"){
			$location.path("/charging");
		}else{
			$rootScope.getNotPay();
		}
	};
})
//有未支付订单页面的控制器
loginApp.controller('notPayController',function($scope,$location){

		//点击未完成的订单
	var notPayCardNo = localStorage.getItem("notPayCardNo");
	localStorage.removeItem("notPayCardNo");
	$scope.goPay=function(){
		// alert("跳转支付页面"+notPayCardNo)
		// $location.path("orderPay/"+notPayCardNo);
		window.location.href=htConfig.hostUrl+"index.html?#/orderPay/"+notPayCardNo;
	};
});


